//
//  UIViewController+PLNavigationBarButton.m
//
//  Created by KimmanYip on 2017/9/7.
//
//

#import "UIViewController+PLNavigationBarButton.h"

@implementation UIViewController (PLNavigationBarButton)

#pragma mark - ----> 修复 iOS7 BarButton 偏移的问题
// Right
- (void)pl_setRightBarButtonItemWithDefaultFixed:(UIBarButtonItem*)item{
    [self pl_setRightBarButtonItemsWithDefaultFixed:item?@[item]:@[]];
}

- (void)pl_setRightBarButtonItem:(UIBarButtonItem*)item fixedPX:(float)px{
    [self pl_setRightBarButtonItems:item?@[item]:@[] fixedPX:px];
}

- (void)pl_setRightBarButtonItemsWithDefaultFixed:(NSArray<UIBarButtonItem*>*)items{
    [self pl_setRightBarButtonItems:items fixedPX:-20];
    
}
- (void)pl_setRightBarButtonItems:(NSArray<UIBarButtonItem*>*)items fixedPX:(float)px{
    self.navigationItem.rightBarButtonItems = [self fixedBarButtonItemsWithItems:items fixedPX:px];
}

// Left
- (void)pl_setLeftBarButtonItemWithDefaultFixed:(UIBarButtonItem*)item{
    [self pl_setLeftBarButtonItemsWithDefaultFixed:item?@[item]:@[]];
}

- (void)pl_setLeftBarButtonItem:(UIBarButtonItem*)item fixedPX:(float)px{
    [self pl_setLeftBarButtonItems:item?@[item]:@[] fixedPX:px];
}

- (void)pl_setLeftBarButtonItemsWithDefaultFixed:(NSArray<UIBarButtonItem*>*)items{
    [self pl_setLeftBarButtonItems:items fixedPX:-20];
    
}
- (void)pl_setLeftBarButtonItems:(NSArray<UIBarButtonItem*>*)items fixedPX:(float)px{
    self.navigationItem.leftBarButtonItems = [self fixedBarButtonItemsWithItems:items fixedPX:px];
}

- (NSArray*)fixedBarButtonItemsWithItems:(NSArray<UIBarButtonItem*>*)items fixedPX:(float)px{
    if (items && items.count > 0) {
        if (px == 0) {
            return items;
        }
        UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
        flexSpacer.width = px;
        return [@[flexSpacer] arrayByAddingObjectsFromArray:items];
    }
    return nil;
}


//创建左上角BarButtonItem按钮
- (void)pl_createLeftBarButtonItemWithTitle:(NSString*)title selector:(SEL)selector{
    UIButton *btn = [self pl_createButtonWithTitle:title color:nil font:nil selector:selector titleEdgeInsets:UIEdgeInsetsZero];
    btn.frame = CGRectMake(0, 0, 60, 44);
    [self pl_createLeftBarButtonItemWithCustomView:btn];
}

- (void)pl_createLeftBarButtonItemWithCustomView:(UIView*)cusView{
    [self pl_setLeftBarButtonItemWithDefaultFixed:[[UIBarButtonItem alloc] initWithCustomView:cusView]];
}

- (void)pl_createLeftBarButtonItemWithNormalImage:(NSString *)normalImage highlightImage:(NSString *)highlightImage selector:(SEL)selector{
    [self pl_createLeftBarButtonItemWithNormalImage:normalImage highlightImage:highlightImage frame:CGRectMake(0, 0, 60, 44) selector:selector];
}

- (void)pl_createLeftBarButtonItemWithNormalImage:(NSString *)normalImage highlightImage:(NSString *)highlightImage frame:(CGRect)frame selector:(SEL)selector{
    UIButton *btn = [self pl_createButtonWithNormalImage:[UIImage imageNamed:normalImage] highlightImage:[UIImage imageNamed:highlightImage] selector:selector];
    btn.frame = frame;
    [self pl_createLeftBarButtonItemWithCustomView:btn];
    
}

//创建右上角BarButtonItem按钮
- (void)pl_createRightBarButtonItemWithTitle:(NSString*)title selector:(SEL)selector{
    UIButton *btn = [self pl_createButtonWithTitle:title color:nil font:nil selector:selector titleEdgeInsets:UIEdgeInsetsZero];
    btn.frame = CGRectMake(0, 0, 60, 44);
    [self pl_createRightBarButtonItemWithCustomView:btn];
}

- (void)pl_createRightBarButtonItemWithCustomView:(UIView*)cusView{
    [self pl_setRightBarButtonItemWithDefaultFixed:[[UIBarButtonItem alloc] initWithCustomView:cusView]];
}

- (void)pl_createRightBarButtonItemWithNormalImage:(NSString *)normalImage highlightImage:(NSString *)highlightImage selector:(SEL)selector
{
    [self pl_createRightBarButtonItemWithNormalImage:normalImage highlightImage:highlightImage frame:CGRectMake(0, 0, 60, 44) selector:selector];
}

//创建右上角BarButtonItem按钮(自定义frame)
- (void)pl_createRightBarButtonItemWithNormalImage:(NSString *)normalImage highlightImage:(NSString *)highlightImage frame:(CGRect)frame selector:(SEL)selector {
    UIButton *btn = [self pl_createButtonWithNormalImage:[UIImage imageNamed:normalImage] highlightImage:[UIImage imageNamed:highlightImage] selector:selector];
    btn.frame = frame;
    [self pl_createRightBarButtonItemWithCustomView:btn];
}

//创建按钮
- (UIButton *)pl_createButtonWithNormalImage:(UIImage *)normalImage selector:(SEL)selector imageEdgeInsets:(UIEdgeInsets)imgInset{
    return [self pl_createButtonWithNormalImage:normalImage highlightImage:nil selector:selector imageEdgeInsets:imgInset];
}

- (UIButton *)pl_createButtonWithNormalImage:(UIImage *)normalImage highlightImage:(UIImage *)highlightImage selector:(SEL)selector
{
    return [self pl_createButtonWithNormalImage:normalImage highlightImage:highlightImage selector:selector imageEdgeInsets:UIEdgeInsetsZero];
}

- (UIButton *)pl_createButtonWithNormalImage:(UIImage *)normalImage highlightImage:(UIImage *)highlightImage selector:(SEL)selector imageEdgeInsets:(UIEdgeInsets)imgInset{
    UIButton *button       = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame           = CGRectMake(0, 0, 44, 44);
    button.imageEdgeInsets = imgInset;
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:highlightImage forState:UIControlStateHighlighted];
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (UIButton *)pl_createButtonWithTitle:(NSString *)normalTitle color:(UIColor *)color font:(UIFont*)font selector:(SEL)selector titleEdgeInsets:(UIEdgeInsets)titleInset{
    UIButton *button       = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame           = CGRectMake(0, 0, 44, 44);
    button.titleEdgeInsets = titleInset;
    button.titleLabel.font = font?font:[UIFont systemFontOfSize:16];
    [button setTitle:normalTitle forState:UIControlStateNormal];
    if (color) {
        [button setTitleColor:color forState:UIControlStateNormal];
    }else{
        [button setTitleColor:self.navigationController.navigationBar.tintColor
                     forState:UIControlStateNormal];
    }
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}


//隐藏左上角按钮
-(void)pl_hideLeftButton {
    self.navigationItem.leftBarButtonItem = nil;
}
//隐藏右上角按钮
-(void)pl_hideRightButton {
    self.navigationItem.rightBarButtonItem = nil;
}



@end
